package com.yanqu.road.server.gameplayer.module.triggerwindow;

import com.yanqu.road.entity.triggerwindow.ActTriggerWindow;
import com.yanqu.road.logic.bussiness.player.TriggerWindowBusiness;
import com.yanqu.road.pb.activity.ActivityTriggerWindowProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;

public class ActTriggerWindowModule extends GeneralModule {
    private ActTriggerWindow actRecordList;

    @Override
    public boolean loadData() {
        actRecordList = TriggerWindowBusiness.getActTriggerWindow(getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (actRecordList != null) {
            if (actRecordList.isInsertOption()) {
                TriggerWindowBusiness.addActTriggerWindow(actRecordList);
            } else if (actRecordList.isUpdateOption()) {
                TriggerWindowBusiness.updateActTriggerWindow(actRecordList);
            }
        }
        return true;
    }

    public ActTriggerWindowModule(GamePlayer player) {
        super(player);
    }

    @Override
    public void loginSendMsg() {
        initUserData();
        syncUserData();
    }

    // 初始化角色数据
    public synchronized void initUserData() {
        // 已有数据
        if (actRecordList != null) {
            return;
        }

        // 初始化
        actRecordList = new ActTriggerWindow();
        actRecordList.setUserId(getUserId());
        actRecordList.setActIdList(new ArrayList<>());
        actRecordList.setInsertOption();
    }

    public void syncUserData() {
        // 无数据
        if (actRecordList == null) {
            return;
        }

        ActivityTriggerWindowProto.ActivityTriggerWindowSyncDataRespMsg.Builder builder = ActivityTriggerWindowProto.ActivityTriggerWindowSyncDataRespMsg.newBuilder();
        ActivityTriggerWindowProto.ActivityTriggerWindowUserDataMsg.Builder dataMsg = ActivityTriggerWindowProto.ActivityTriggerWindowUserDataMsg.newBuilder();
        dataMsg.addAllActivityIdList(actRecordList.getActIdList());
        builder.setWindowUserData(dataMsg);
        player.sendPacket(ClientProtocol.U_SYNC_ACTIVITY_TRIGGER_WINDOW, builder);
    }

    public ActTriggerWindow getActRecordList() {
        return actRecordList;
    }

    public void setActRecordList(ActTriggerWindow actRecordList) {
        this.actRecordList = actRecordList;
    }

    /**
     * 获取单个活动是否弹窗验证
     * @param activityId
     * @return
     */
    public int getData(int activityId) {
        ActTriggerWindow userData = getActRecordList();
        boolean isTrigger = userData.getActIdList().contains(activityId);
        ActivityTriggerWindowProto.ActivityTriggerWindowGetDataRespMsg.Builder builder = ActivityTriggerWindowProto.ActivityTriggerWindowGetDataRespMsg.newBuilder();
        builder.setRet(0);
        builder.setIsTrigger(isTrigger);
        player.sendPacket(ClientProtocol.U_GET_ACTIVITY_TRIGGER_WINDOW, builder);
        return 0;
    }

    /**
     * 触发指定活动的一次性弹窗
     * @param activityId
     * @return
     */
    public int setData(int activityId) {
        ActTriggerWindow userData = getActRecordList();
        boolean isTrigger = userData.getActIdList().contains(activityId);
        if(isTrigger){
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }
        userData.getActIdList().add(activityId);
        userData.setUpdateOption();

        ActivityTriggerWindowProto.ActivityTriggerWindowSetRespMsg.Builder builder = ActivityTriggerWindowProto.ActivityTriggerWindowSetRespMsg.newBuilder();
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_ADD_ACTIVITY_TRIGGER_WINDOW, builder);
        return 0;
    }
}
